package AdminService

import (
	"gdshop-admin-go-api/app/request/BaseReq"
	"gdshop-admin-go-api/library/response"
	toolsDb "gdshop-admin-go-api/library/tools/db"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"strings"
	"time"
)

func PersonUpdate(r *ghttp.Request, req *BaseReq.I) *response.JsonResponse {
	// 处理 avatar 如果提交了HTTP地址的话，剔除
	avatar := r.GetString("avatar")
	tmp := strings.Split(avatar, "/upload/")
	avatar = tmp[len(tmp)-1]
	// 处理 密码的提交

	res, err := toolsDb.GetUnSafaTableAddDeleteWhere(r.GetCtx(), req.TableName).Where(
		"id",
		req.AdminId,
	).Update(g.Map{
		"avatar":    "/upload/" + avatar,
		"nickname":  r.GetString("nickname"),
		"update_at": time.Now().Unix(),
	})
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	row, err := res.RowsAffected()
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}
	if row < 1 {
		return response.FailByRequestMessage(nil, "编辑失败")
	}
	return response.SuccessByRequestMessage(nil, "编辑成功")
}
